const app = getApp()

Page({
    data: {
        theme: app.globalData.theme, //全局颜色变量
        CustomBar: app.globalData.CustomBar,
        // 1：微信支付，2：余额支付
        paymentType: 1,
        userInfo: {
            // 余额
            balance: 0,
            // 赠送金额
            balanceGift: 0
        },
        loading: false,
        orderId: null,
        sourcePage: "createOrder",
        orderInfo: {}
    },
    onShow() {
        app.initPage()
            .then(res => {
                this.userInfoGet()
                this.orderGet(this.data.orderId)
            })
    },
    onLoad(options) {
        var that = this
        that.setData({
            theme: app.globalData.theme
        });
        this.setData({
            orderId: options.orderId,
            sourcePage: options.sourcePage,
            paymentPrice: options.paymentPrice
        })
    },
    changePayType(e) {
        let that = this
        let paymentType = e.currentTarget.dataset.paymentType
        if (paymentType == 1 || (paymentType == 2 && (that.data.userInfo.balance + that.data.userInfo.balanceGift) > 0)) {
            this.setData({
                paymentType: paymentType
            })
        }
    },
    orderGet(orderId) {
        let that = this
        app.api.orderGet(orderId)
            .then(res => {
                let orderInfo = res.data
                if (!orderInfo) {
                    wx.showToast({
                        title: '无效订单',
                        icon: 'none',
                        duration: 5000
                    })
                    return
                }
                this.setData({
                    orderInfo: orderInfo
                })
            })
    },
    //获取商城用户信息
    userInfoGet() {
        app.api.userInfoGet()
            .then(res => {
                this.setData({
                    userInfo: res.data
                })
            })
    },
    confirm: function confirm() {
        if (this.data.paymentType === '3') {
            if (!this.data.userInfo.balance || this.data.userInfo.balance + this.data.userInfo.balanceGift < this.data.orderInfo.paymentPrice) {
                wx.showToast({title: "余额不足", icon: "none", duration: 1000});
                return;
            }
        }
        this.unifiedOrder();
    },
    unifiedOrder() {
        this.setData({
            loading: true
        })
        var that = this
        let orderInfo = this.data.orderInfo
        app.api.unifiedOrder({
            tradeType: 'JSAPI',
            id: orderInfo.id,
            paymentType: that.data.paymentType
        })
            .then(res => {
                this.setData({
                    loading: false
                })
                if (orderInfo.paymentPrice <= 0 || that.data.paymentType == "2") {
                    // 0元付款 或 余额支付
                    that.successCallback(res);
                } else {
                    let payData = res.data
                    wx.requestPayment({
                        timeStamp: payData.timeStamp,
                        nonceStr: payData.nonceStr,
                        package: payData.packageValue,
                        signType: payData.signType,
                        paySign: payData.paySign,
                        success: function success(res) {
                            that.successCallback(res);
                        },
                        fail: function fail(res) {
                            console.log('fail:' + JSON.stringify(res));
                        },
                        complete: function complete(res) {
                            console.log(res);
                        }
                    })
                }
            }).catch(() => {
            this.setData({
                loading: false
            })
        })
    },
    //统一支付成功回调
    successCallback(res) {
        switch (this.data.sourcePage) {
            case 'createOrder':
                wx.redirectTo({
                    url: '/pages/order/order-pay-result/index?orderId=' + this.data.orderId
                });
                break;
        }
    }
})
